package com.damon.bussearch.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class StopFactory implements StopDao{

//	private static final String TAG = "StopFactory";
	private static StopFactory self ;
	private Context context;
	
	private StopDatabaseHelper dbHelper;
		
	private StopFactory(Context context){
		this.context = context;
		dbHelper = new StopDatabaseHelper(context);
	}
	
	public static StopFactory getInstance(Context context){
		if(self== null){
			self = new StopFactory(context);
		}
		return self;
	}
	
	@Override
	public boolean deleteStop(long id) {
		SQLiteDatabase  db = dbHelper.getWritableDatabase();
		if(db.delete(Stop.TABLE_NAME, Stop._ID+"=?", new String[]{id+""})>0){
			db.close();
			return true;
		}else{
			db.close();
			return false;
		}
		
	}

	@Override
	public Stop getStopById(long id) {
		SQLiteDatabase  db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(true, Stop.TABLE_NAME, new String[]{Stop.NAME}, Stop._ID+"="+id, null, null, null, null, null);
		if(cursor!=null){
			cursor.moveToFirst();
			int stopIndex = cursor.getColumnIndex(Stop.NAME);
			if(stopIndex > -1){
				String stop = cursor.getString(stopIndex);
				cursor.close();
				db.close();
				return new Stop(stop);
			}else{
//				Log.e(TAG,Stop.NAME+" column does not exsit. or "+Stop.AUDIO_URI+" column does not exsit");
				cursor.close();
				db.close();
				return null;
			}
		}else{
			db.close();
			return null;
		}
	}

	@Override
	public List<String> getAllStops() {
		SQLiteDatabase  db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(true, Stop.TABLE_NAME, new String[]{Stop.NAME}, null, null, null, null, null, null);
		List<String> notes = new ArrayList<String>();
		if(cursor!=null){
			cursor.moveToFirst();
			while(!cursor.isAfterLast()){
				int stopIndex = cursor.getColumnIndex(Stop.NAME);
				if(stopIndex > -1 ){
					String stop = cursor.getString(stopIndex);
					notes.add(stop);
				}else{
//					Log.e(TAG,Stop.NAME+" column does not exsit. or "+Stop.AUDIO_URI+" column does not exsit");
					cursor.close();
					db.close();
					return null;
				}
				cursor.moveToNext();
			}
			cursor.close();
			db.close();
			return notes;
		}else{
			db.close();
			return null;
		}	
	}

	@Override
	public boolean insertStop(String stop) {
		SQLiteDatabase  db = dbHelper.getWritableDatabase();
		ContentValues value = new ContentValues();
		value.put(Stop.NAME, stop);
		try{
			db.insertOrThrow(Stop.TABLE_NAME, Stop._ID, value);
			db.close();
			return true;
		}catch(SQLException e){
			e.printStackTrace();
			db.close();
			return false;
		}
	}

	@Override
	public boolean updateStop(long id, String stop) {
		SQLiteDatabase  db = dbHelper.getWritableDatabase();
		ContentValues value = new ContentValues();
		value.put(Stop.NAME, stop);
		if(db.update(Stop.TABLE_NAME, value, Stop._ID+"="+id, null)>0){
			db.close();
			return true;
		}else{
			db.close();
			return false;
		}
	}

	@Override
	public boolean deleteStop(String name) {
		SQLiteDatabase  db = dbHelper.getWritableDatabase();
		if(db.delete(Stop.TABLE_NAME, Stop.NAME+"=?", new String[]{name+""})>0){
			db.close();
			return true;
		}else{
			db.close();
			return false;
		}
	}
	
	public boolean deleteStops(){
		SQLiteDatabase  db = dbHelper.getWritableDatabase();
		if(db.delete(Stop.TABLE_NAME, null, null)>0){
			db.close();
			return true;
		}else{
			db.close();
			return false;
		}
	}
	
	public void reset(){
		if(dbHelper!=null){
			dbHelper.close();
		}
		dbHelper = new StopDatabaseHelper(context);
	}
	
	public void close(){
		if(dbHelper!=null){
			dbHelper.close();
		}
	}

	@Override
	protected void finalize() throws Throwable {
		if(dbHelper!=null){
			dbHelper.close();
		}
		super.finalize();
	}

	@Override
	public List<String> getStopByKeyword(String key) {
		SQLiteDatabase  db = dbHelper.getWritableDatabase();
		String sql = "SELECT name FROM stop_history WHERE name LIKE ('%"+key+"%')";
		Cursor cursor =db.rawQuery(sql, null);
//		Cursor cursor = db.query(true, Stop.TABLE_NAME, new String[]{Stop.NAME}, null, null, null, null, null, null);
		List<String> stops = new ArrayList<String>();
		if(cursor!=null){
			cursor.moveToFirst();
			while(!cursor.isAfterLast()){
				int stopIndex = cursor.getColumnIndex(Stop.NAME);
				if(stopIndex > -1 ){
					String stop = cursor.getString(stopIndex);
					stops.add(stop);
				}else{
//					Log.e(TAG,Stop.NAME+" column does not exsit. or "+Stop.AUDIO_URI+" column does not exsit");
					cursor.close();
					db.close();
					return null;
				}
				cursor.moveToNext();
			}
			cursor.close();
			db.close();
			return stops;
		}else{
			db.close();
			return null;
		}	
	}


	
	
}
